package com.shawn.jetpackstady.navigation;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.NavDestination;
import androidx.navigation.NavOptions;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.shawn.jetpackstady.R;

public class NavigationMenuActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener, NavController.OnDestinationChangedListener {
    NavController navController;
    BottomNavigationView bottomNavView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_menu);
        initNav();
    }

    private void initNav() {
        navController = Navigation.findNavController(this, R.id.my_nav_host_fragment);
        bottomNavView = (BottomNavigationView) findViewById(R.id.bav_main_menu);
        NavigationUI.setupWithNavController(bottomNavView, navController);
        bottomNavView.setItemIconTintList(null);
        bottomNavView.setOnNavigationItemSelectedListener(this);
        navController.addOnDestinationChangedListener(this);
    }

    @Override
    public boolean onSupportNavigateUp() {
        return navController.navigateUp();
    }



    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        int itemId = menuItem.getItemId();
        switch (itemId) {
            case R.id.page1Fragment:
                if (menuItem.isChecked()){
                    return false;
                }
                navController.navigate(R.id.action_page1);
                break;
            case R.id.page2Fragment:
                if (menuItem.isChecked()){
                    return false;
                }
                navController.navigate(R.id.action_page2);
                break;
        }
        return false;
    }

    @Override
    public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
        String label = String.valueOf(destination.getLabel());
    }
}
